#include "Stack.h"

Stack* StackCreate() {
    Stack* obj;
    obj = (Stack*)malloc(sizeof(Stack));
    obj->a = (int*)malloc(sizeof(int) * CAPACITY);
    obj->top = -1;
    obj->capacity = CAPACITY;
    return obj;
}

void StackPush(Stack* obj, int x) {
    if (obj->top + 1 == obj->capacity) {
        int* tmp = obj->a;
        tmp = (int*)realloc(obj->a, sizeof(int) * 2 * obj->capacity);
        if (!tmp) {
            printf("realloc fail\n");
            return;
        }
        obj->a = tmp;
        obj->capacity *= 2;
    }
    obj->top++;
    obj->a[obj->top] = x;
}

int StackPop(Stack* obj) {
    if (obj->top >= 0) {
        int num = obj->a[obj->top];
        obj->top--;
        return num;
    }
    return -1;
}

int StackPeek(Stack* obj) {
    if (obj->top >= 0) {
        return obj->a[obj->top];
    }
    return -1;
}

bool StackEmpty(Stack* obj) {
    return obj->top < 0;
}

void StackDestory(Stack* obj) {
    free(obj->a);
    obj->a = NULL;
    free(obj);
    obj = NULL;
}